package com.bw.controller;

import com.bw.entity.Dict;
import com.bw.entity.GoodsInfo;
import com.bw.entity.GoodsQuery;
import com.bw.entity.User;
import com.bw.mapper.GoodsMapper;
import com.bw.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

@Controller
public class ListController {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsService goodsService;

    /**
     * 浏览器 在访问 所有的controller 时 会自动携带cookie
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("list.do")
    public String list(GoodsQuery query,Integer pageNum, Model model, HttpServletRequest request){
        // 获取当前登录用户
        // 从request中 获取cookie  ，从cookie 过滤出token  ，解密token得到 user对象
        User loginUser = TokenUtils.getLoginUserFromRequest(request, User.class);

        model.addAttribute("name",loginUser.getName());
        model.addAttribute("nickName",loginUser.getNickName());
        model.addAttribute("avatarUrl",loginUser.getAvatarUrl());

        //1. 渲染查询条件的下拉框
       List<Dict> brandList =  goodsMapper.listBrand();
       model.addAttribute("brandList",brandList);
       List<Dict> categoryList =  goodsMapper.listCategory();
       model.addAttribute("categoryList",categoryList);
        // 2. 分页查询

        PageHelper.startPage(pageNum == null ? 1 :pageNum,3);
        List<GoodsInfo> list =  goodsMapper.selectList(query);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);

        //3， 条件不丢失

        model.addAttribute("query",query);
        return "list";
    }


    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        //1. 渲染查询条件的下拉框
        List<Dict> brandList =  goodsMapper.listBrand();
        model.addAttribute("brandList",brandList);
        List<Dict> categoryList =  goodsMapper.listCategory();
        model.addAttribute("categoryList",categoryList);
        return "add";
    }
    @RequestMapping("/add.do")
    public String add(@Valid GoodsInfo goodsInfo, BindingResult result, Model model,HttpServletRequest request){
        if (result.hasErrors()){

            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            //1. 渲染查询条件的下拉框
            List<Dict> brandList =  goodsMapper.listBrand();
            model.addAttribute("brandList",brandList);
            List<Dict> categoryList =  goodsMapper.listCategory();
            model.addAttribute("categoryList",categoryList);
            return "add";
        }
        //商品编号由系统生成，且不能重复
        String num = UUID.randomUUID().toString();
        goodsInfo.setNum(num);
        //添加人为当前登录用户
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);
        Integer userId = userFromRequest.getId();
        goodsInfo.setCreateUser(userId);

        goodsService.add(goodsInfo);
        return "redirect:list.do";
    }

}
